package com.shitou.shitoumember.vo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

public class JDPhoneSpider {
    public static void main(String[] args) throws Exception {
        // 设置代理
        String proxyHost = "39.144.228.233";
        int proxyPort = 80;
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

        // 设置要爬取的京东手机商品页面url
        String phoneUrl = "https://list.jd.com/list.html?cat=9987,653,655";
        URL url = new URL(phoneUrl);
        URLConnection conn = url.openConnection(proxy);

        // 设置请求头
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:72.0) Gecko/20100101 Firefox/72.0");

        // 发送请求并获取响应结果
        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        // 关闭连接和流
        br.close();
        isr.close();
        is.close();
    }
}
